package com.haiya.web.controller;

import com.haiya.application.command.CargoCmdService;
import com.haiya.application.command.cmd.CargoBookCommand;
import com.haiya.application.query.CargoQueryService;
import com.haiya.application.query.dto.CargoDTO;
import com.haiya.application.query.qry.CargoFindbyCustomerQry;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author qiaoguoqiang
 * CQRS: 命令查询职责分离
 */
@RestController
@RequestMapping("/cargo")
public class CargoController {
    @Autowired
    CargoQueryService cargoQueryService;

    @Autowired
    CargoCmdService cargoCmdService;

    @GetMapping("")
    public List<CargoDTO> queryCargos(
            @RequestParam(value = "phone", required = false) String phone) {
        if (!StringUtils.isEmpty(phone)) {
            CargoFindbyCustomerQry qry = new CargoFindbyCustomerQry();
            qry.setCustomerPhone(phone);
            return cargoQueryService.queryCargos(qry);
        }
        return cargoQueryService.queryCargos();
    }

    @GetMapping("/{cargoId}")
    public CargoDTO cargo(@PathVariable String cargoId) {
        return cargoQueryService.getCargo(cargoId);
    }

    @PostMapping("")
    public void book(@RequestBody CargoBookCommand cargoBookCommand) {
        cargoCmdService.bookCargo(cargoBookCommand);
    }
}
